package com.itedge.solutionmanager.web.taskhandler.impl.customer;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.infrastructure.stereotype.WebHandler;
import com.itedge.solutionmanager.taskdata.impl.customer.EditCustomerTaskData;
import com.itedge.solutionmanager.web.convert.ISOFormattingConversionServiceBean;
import com.itedge.infrastructure.web.taskhandler.TaskHandlerException;
import com.itedge.infrastructure.web.taskhandler.impl.AbstractTaskDataHandler;

@WebHandler
public class EditCustomerTaskDataHandler extends AbstractTaskDataHandler<EditCustomerTaskData, Customer> {

    private ITaskDataEntityService<EditCustomerTaskData> editCustomerTaskDataService;

    @Autowired
    protected EditCustomerTaskDataHandler(ISOFormattingConversionServiceBean conversionService,
    		IProcessService<Customer> customerProcessService,
    		ITaskDataEntityService<EditCustomerTaskData> editCustomerTaskDataService,
    		ITaskService<EditCustomerTaskData, Customer> editCustomerTaskService,
			MessageSource messageSource) {
		super(conversionService, customerProcessService, editCustomerTaskDataService, 
				editCustomerTaskService, messageSource);
		this.editCustomerTaskDataService = editCustomerTaskDataService;
	}

	@Override
	protected Class<EditCustomerTaskData> getTaskDataClass() {
		return EditCustomerTaskData.class;
	}
    
	@Override
	public Map<String,Object> loadTaskData(String taskId, Customer linkedCustomer, Boolean forHistory) {
    	EditCustomerTaskData taskData = editCustomerTaskDataService.findEntityByProcessTaskId(taskId);;
    	if (taskData == null) {
    	    taskData = new EditCustomerTaskData(); 
        	//set old values
        	Customer customer = linkedCustomer != null ? linkedCustomer : new Customer();
        	rememberEditTaskOldData(taskData, customer);
    	} 
    	taskData.setTaskId(taskId);
    	Map<String,Object> data = new HashMap<String,Object>();
    	data.put("taskData", taskData);
    	return data;	
	}
	
	@Override
	public EditCustomerTaskData bindTaskData(HttpServletRequest request, Customer linkedCustomer) 
			throws TaskHandlerException {
		EditCustomerTaskData taskData = super.bindTaskData(request, linkedCustomer);
		rememberEditTaskOldData(taskData, linkedCustomer);
		return taskData;
	}
	
	private void rememberEditTaskOldData(EditCustomerTaskData taskData, Customer customer) {
    	// remember old values
    	taskData.setOldDescription(customer.getDescription());
    	taskData.setOldName(customer.getName());
    	taskData.setOldIco(customer.getIco());
    	taskData.setOldDic(customer.getDic());
    	taskData.setOldIcdph(customer.getIcdph());
	}
	
}
